// // Copyright (c) 2009 All Right Reserved // // Stephen Toub // stoub@microsoft.com // 2009-01-01 // Contains ... using System; using System.IO; using System.Text; using LargoCommon.Music; namespace LargoCommon.Midi { /// A key signature meta event message. [Serializable] public sealed class MetaKeySignature : MetaEvent { #region Fields /// The meta id for this event. private const byte EventMetaId = 0x59; /// Number of sharps or flats in the signature. private TonalityKey key; /// TonalityGenus genus of the signature. private TonalityGenus tonalityGenus; #endregion #region Constructors /// Initializes a new instance of the MetaKeySignature class. /// The amount of time before this event. /// Key of the signature. /// Tonality genus of the signature. public MetaKeySignature(long deltaTime, TonalityKey key, TonalityGenus tonalGenus) : base(deltaTime, EventMetaId) { this.Key = key; this.TonalityGenus = tonalGenus; } #endregion #region Properties /// Gets the numerator for the event. /// General musical property. public TonalityKey Key { get => this.key; private set { var k = (TonalityKey)(sbyte)value; if (!Enum.IsDefined(typeof(TonalityKey), k)) { return; //// throw new ArgumentOutOfRangeException(nameof(value), value, "Not a valid key."); } this.key = k; } } /// Gets or sets the denominator for the event. /// General musical property. private TonalityGenus TonalityGenus { get => this.tonalityGenus; set { if (!Enum.IsDefined(typeof(TonalityGenus), value)) { this.tonalityGenus = TonalityGenus.Major; //// throw new ArgumentOutOfRangeException("value", value, "Not a valid tonality."); } this.tonalityGenus = value; } } #endregion #region To String /// Generate a string representation of the event. /// A string representation of the event. public override string ToString() { var sb = new StringBuilder(); sb.Append(base.ToString()); sb.Append("\t"); sb.Append(this.key); sb.Append("\t"); sb.Append(this.TonalityGenus); return sb.ToString(); } #endregion #region Methods /// Write the event to the output stream. /// The stream to which the event should be written. public override void Write(Stream outputStream) { if (outputStream == null) { return; } //// Write out the base event information base.Write(outputStream); //// Event data outputStream.WriteByte(0x02); outputStream.WriteByte((byte)this.key); outputStream.WriteByte((byte)this.tonalityGenus); } #endregion } }